//
//  WJVistorView.swift
//  WJWeibo
//
//  Created by Jie on 15/12/16.
//  Copyright © 2015年 周文杰. All rights reserved.
//

import UIKit

protocol WJVistorViewDelegate: NSObjectProtocol {
    
    //点击注册
    func vistorViewRegister()
    //点击登录
    func vistorViewLogin()
}


class WJVistorView: UIView {

    
    //代理
    weak var vistorViewDelegate:WJVistorViewDelegate?


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //加载图片
        prepareUI()
        
    }

    
    
    
    //MARK: - 加载UI
    private func prepareUI(){
        
        // 背景颜色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        //添加控件
        addSubview(iconView)
        addSubview(shadeView)
        addSubview(homeView)
        addSubview(textLab)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //关掉Autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        shadeView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        textLab.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //设置lab的行数
        textLab.numberOfLines = 0
        
        
        
        //设置约束
        //iconView
        
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -50))
        
        //shadeView
        self.addConstraint(NSLayoutConstraint(item: shadeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: shadeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        //homeView
        
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 5))
        
        
        //textLab
        

        
        self.addConstraint(NSLayoutConstraint(item: textLab, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: textLab, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 100))
        //宽
        self.addConstraint(NSLayoutConstraint(item: textLab, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 250))
        
        //registerBtn
        
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: textLab, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLab, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        //宽
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 100))
        //高
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 35))
        
        //loginBtn
        
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: textLab, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLab, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        //宽
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 100))
        //高
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 35))
        
        
    }
    
    ///根据控制器切换图片
    func changeUI(iconName : String, text : String) {
        homeView.hidden = true
        iconView.image = UIImage(named: iconName)
        textLab.text = text
        textLab.sizeToFit()
    
        //把遮罩层放到最下面
        sendSubviewToBack(shadeView)
        
    }
    
    
    
    //MARK: - 转轮动画
    ///开始动画
    func starResellAnim() {
        //核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation");
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //让动画不停止（切换其他控制器或者回到桌面）
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: "homeRotation")
    }
    ///暂停动画
    func pauseResellAnim () {
        //记录暂停时间
        let offsetTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        //设置动画速度为0
        iconView.layer.speed = 0
        //设置动画偏移时间
        iconView.layer.timeOffset = offsetTime
        
        
    }
    ///恢复动画
    func resumeResellAnim () {
        //获得动画偏移时间
        let offsetTime = iconView.layer.timeOffset
        //设置动画速度
        iconView.layer.speed = 1
        //重设动画偏移时间
        iconView.layer.timeOffset = 0
        //重置动画开始时间
        iconView.layer.beginTime = 0
        //计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - offsetTime
        //设置开始时间
        iconView.layer.beginTime = timeSincePause 
    }
    
    
    //MARK: - 懒加载控件
    
    //转轮
    private lazy var iconView : UIImageView = {
        var icon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return icon
    }()
    
    //家
    private lazy var homeView : UIImageView = {
        var icon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return icon
    }()
    //文字
    private lazy var textLab : UILabel = {
       var lab = UILabel()
        lab.text = "关注一些人,看看这里有什么惊喜!"
        lab.textColor = UIColor.darkGrayColor()
        return lab
        
    }()
    //注册按钮x
    private lazy var registerBtn : UIButton = {
        var btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.sizeToFit()
        //添加点击时间
        btn.addTarget(self, action: "didRegister", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    //登录按钮
    private lazy var loginBtn : UIButton = {
        var btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "didLogin", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    //遮罩层
    private lazy var shadeView : UIImageView = {
       var img = UIImageView()
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return img
    }()
    
    
    
    //MARK: - 按钮点击事件
    
    //点击注册
    func didRegister() {
        vistorViewDelegate?.vistorViewRegister()
    }
    
    //点击登录
    func didLogin() {
        vistorViewDelegate?.vistorViewLogin()
        
    }

}
